package elcs.aop;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import elcs.board.model.BoardVO;
import elcs.board.model.ListVO;
import elcs.rank.model.RankService;

@Component
@Aspect
public class SystemAop {

	private Log log = LogFactory.getLog(getClass());
	@Resource(name = "rankServiceImpl")
	private RankService rankService;

	@Around("execution(* elcs.board.model.BoardServiceImpl.search*(..))")
	public Object statisticsSearchWord(ProceedingJoinPoint point)
			throws Throwable {

		String cn = point.getTarget().getClass().getName();
		String mn = point.getSignature().getName();
		Object retValue = "";

		retValue = point.proceed();
		Object param[] = point.getArgs();
		String word = "";

		for (int i = 0; i < param.length; i++) {
			Map map = (Map) param[i];
			// System.out.println(map.containsKey("title"));
			if (map.containsKey("title")) {
				word = (String) map.get("title");
			} else if (map.containsKey("content")) {
				word = (String) map.get("content");
			} else if (map.containsKey("id")) {
				word = (String) map.get("id");
			}

		}
		int result = rankService.duplicateWord(word);

		if (result == 0) {
			rankService.insertRank(word);
		} else {
			rankService.updateRank(word);
		}

		return retValue;
	}
}